<template>
	<view class="delivery-address">
		<g-navbar title="收货地址" backgroundColor="none" />
		<scroll-view scroll-y class="delivery-address-list">
			<view class="delivery-address-item" v-for="item in addressList" :key="item.id"
				@click="onClickAddressItem(item.id)">
				<view class="delivery-user">
					<view class="user-name">
						{{item.name}}
					</view>
					<view class="user-phone">
						{{item.phone}}
					</view>
				</view>
				<view class="address-detail">
					<view class="address">
						{{`${item.pro} ${item.city} ${item.area} ${item.address}`}}
					</view>
					<i class="iconfont icon-modify icon" @click="edit(item.id)" />
				</view>
				<view class="default-icon" v-if="item.default === DEFAULT_TYPE.SET">
					默认
				</view>
			</view>
			<g-placeholder v-if="!addressList.length" text="暂无地址信息" :icon="PLACEHOLDER_ICON.ADDRESS" />
			<view style="position: relative; margin-top: 40rpx;" :style="{top:addressList.length?'0':'600rpx'}">
				<g-button btn_text="添加新地址" icon="add" :height="100" :width="686" :borderRadius="24" @click="jump" />
			</view>
		</scroll-view>
	</view>
</template>

<script lang="ts" setup>
	import { PLACEHOLDER_ICON } from '@/components/global/g-placeholder/types';
	import REST_URL from '@/http/http.restUrl';
	import { useStore } from 'vuex';
	import { DEFAULT_TYPE } from '@/enum/address';
	import { Ref, ref } from "vue";
	import { IAddress } from '@/types/address';
	import { onLoad } from '@dcloudio/uni-app';
	import { GET_STORE_USERINFO } from '@/store/gettersType';

	let isSelect : boolean = false;

	onLoad((e) => {
		if (e.isSelect) {
			isSelect = true;
		}
	})

	const addressList : Ref<IAddress[]> = ref([]);

	const jump = () => {
		uni.router.route('/pages_address/add_delivery_address/addDeliveryAddress')
	}

	const store = useStore();

	uni.$off('updateDeliveryAddressList');
	uni.$on('updateDeliveryAddressList', () => {
		getDeliveryAddress();
	})

	const getDeliveryAddress = async () => {
		await uni.loginManger.$interceptor;
		const result : IAddress[] = (await uni.http.POST(REST_URL.getDeliveryAddress, {
			customer_id: store.getters[GET_STORE_USERINFO].id,
		})).data;
		console.log(result)
		addressList.value = result ? result : [];
	}

	const onClickAddressItem = (id : number) => {
		if (isSelect) {
			uni.navigateBack({
				success: () => {
					uni.$emit('selectAddress', id);
				}
			})
		}
	}

	const edit = (id : string) => {
		uni.router.route({
			url: '/pages_address/add_delivery_address/addDeliveryAddress',
			params: {
				id
			}
		})
	}

	getDeliveryAddress();
</script>

<style scoped lang="scss">
	@import "./deliveryAddress.scss";
</style>